package com.eftimoff.ohosplayer.actions.property;

import com.eftimoff.ohosplayer.actions.BaseAction;

import ohos.agp.animation.Animator;
import ohos.agp.components.Component;

/**
 * Action that animates the properties of the view.
 * <p/>
 * Created by georgi.eftimov on 4/15/2015.
 */
public class PropertyAction extends BaseAction {

    private float fromTranslationX = 0;

    private float toTranslationX = 0;

    private float fromTranslationY = 0;

    private float toTranslationY = 0;

    private float fromScaleY = 1f;

    private float toScaleY = 1f;

    private float fromScaleX = 1f;

    private float toScaleX = 1f;

    private float fromAlpha = 1f;

    private float toAlpha = 1f;

    private float fromRotation;

    private float toRotation;

    public float getTranslationX() {
        return fromTranslationX;
    }

    @Override
    public void init(Component view) {
        setToTranslationX(getTranslationX(view));
        setTranslationX(view, getTranslationX());
        setToTranslationY(getTranslationY(view));
        setTranslationY(view, getTranslationY());
        setToScaleX(getScaleX(view));
        setScaleX(view, getScaleX());
        setToScaleY(getScaleY(view));
        setScaleY(view, getScaleY());
        setToAlpha(getAlpha(view));
        setAlpha(view, getAlpha());
        setToRotation(getRotation(view));
        setRotation(view, getRotation());
    }

    @Override
    public void animate(Component view) {
        ViewPropertyAnimator.animate(view)
            .translationX(toTranslationX)
            .translationY(toTranslationY)
            .scaleY(toScaleY)
            .scaleX(toScaleX)
            .alpha(toAlpha)
            .rotation(toRotation)
            .setDuration(getDuration())
            .setStartDelay(getDelay())
        .setInterpolator(getInterpolator())
            .start();
    }

    public float getTranslationY() {
        return fromTranslationY;
    }

    public float getScaleY() {
        return fromScaleY;
    }

    public float getRotation() {
        return fromRotation;
    }

    public float getScaleX() {
        return fromScaleX;
    }

    public float getAlpha() {
        return fromAlpha;
    }

    public static final class Builder {
        private Component view;

        private boolean animateAlone;

        private int duration = 300;

        private int delay = 0;

        private int interpolator = Animator.CurveType.ACCELERATE;

        private float fromTranslationX = 0;

        private float toTranslationX = 0;

        private float fromTranslationY = 0;

        private float toTranslationY = 0;

        private float fromScaleY = 1f;

        private float toScaleY = 1f;

        private float fromScaleX = 1f;

        private float toScaleX = 1f;

        private float fromAlpha = 1f;

        private float toAlpha = 1f;

        private float rotation;

        private Builder(final Component view) {
            this.view = view;
        }

        public PropertyAction build() {
            return new PropertyAction(this);
        }

        public Builder duration(int duration) {
            this.duration = duration;
            return this;
        }

        public Builder delay(int delay) {
            this.delay = delay;
            return this;
        }

        public Builder translationX(float fromTranslationX) {
            this.fromTranslationX = fromTranslationX;
            return this;
        }

        public Builder translationY(float fromTranslationY) {
            this.fromTranslationY = fromTranslationY;
            return this;
        }

        public Builder scaleY(float fromScaleY) {
            this.fromScaleY = fromScaleY;
            return this;
        }

        public Builder scaleX(float fromScaleX) {
            this.fromScaleX = fromScaleX;
            return this;
        }

        public Builder alpha(float fromAlpha) {
            this.fromAlpha = fromAlpha;
            return this;
        }

        public Builder interpolator(int interpolator) {
            this.interpolator = interpolator;
            return this;
        }

        public Builder animateAlone(final boolean shouldWait) {
            this.animateAlone = shouldWait;
            return this;
        }

        public Builder rotation(final float rotation) {
            this.rotation = rotation;
            return this;
        }
    }

    public static float getTranslationX(Component view) {
        return view.getTranslationX();
    }

    public static void setTranslationX(Component view, float translationX) {
        view.setTranslationX(translationX);
    }

    public static float getTranslationY(Component view) {
        return view.getTranslationY();
    }

    public static void setTranslationY(Component view, float translationY) {
        view.setTranslationY(translationY);
    }

    public static float getScaleX(Component view) {
        return view.getScaleX();
    }

    public static void setScaleX(Component view, float scaleX) {
        view.setScaleX(scaleX);
    }

    public static float getScaleY(Component view) {
        return view.getScaleY();
    }

    public static void setScaleY(Component view, float scaleY) {
        view.setScaleY(scaleY);
    }

    public static float getAlpha(Component view) {
        return view.getAlpha();
    }

    public static void setAlpha(Component view, float alpha) {
        view.setAlpha(alpha);
    }

    public static float getRotation(Component view) {
        return view.getRotation();
    }

    public static void setRotation(Component view, float rotation) {
        view.setRotation(rotation);
    }

    public static Builder newPropertyAction(final Component view) {
        return new Builder(view);
    }

    private PropertyAction(Builder builder) {
        super(builder.view);
        setDuration(builder.duration);
        setDelay(builder.delay);
        setAnimateAlone(builder.animateAlone);
        setInterpolator(builder.interpolator);
        this.fromTranslationX = builder.fromTranslationX;
        this.toTranslationX = builder.toTranslationX;
        this.fromTranslationY = builder.fromTranslationY;
        this.toTranslationY = builder.toTranslationY;
        this.fromScaleY = builder.fromScaleY;
        this.toScaleY = builder.toScaleY;
        this.fromScaleX = builder.fromScaleX;
        this.toScaleX = builder.toScaleX;
        this.fromAlpha = builder.fromAlpha;
        this.toAlpha = builder.toAlpha;
        this.fromRotation = builder.rotation;
    }

    private void setFromTranslationX(float fromTranslationX) {
        this.fromTranslationX = fromTranslationX;
    }

    private void setToTranslationX(float toTranslationX) {
        this.toTranslationX = toTranslationX;
    }

    private void setFromTranslationY(float fromTranslationY) {
        this.fromTranslationY = fromTranslationY;
    }

    private void setToTranslationY(float toTranslationY) {
        this.toTranslationY = toTranslationY;
    }

    private void setFromScaleY(float fromScaleY) {
        this.fromScaleY = fromScaleY;
    }

    private void setToScaleY(float toScaleY) {
        this.toScaleY = toScaleY;
    }

    private void setFromScaleX(float fromScaleX) {
        this.fromScaleX = fromScaleX;
    }

    private void setToScaleX(float toScaleX) {
        this.toScaleX = toScaleX;
    }

    private void setFromAlpha(float fromAlpha) {
        this.fromAlpha = fromAlpha;
    }

    private void setToAlpha(float toAlpha) {
        this.toAlpha = toAlpha;
    }

    private void setToRotation(float toRotation) {
        this.toRotation = toRotation;
    }

    private void setFromRotation(float fromRotation) {
        this.fromRotation = fromRotation;
    }

}
